package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.constant.MqConstant;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /*
    分页查询商品
     */
    @GetMapping("/list")
    public PageDTO<Item> list(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        log.warn("page:{}, size:{}", page, size);
        Page<Item> mpPage = new Page<>(page, size);
        Page<Item> pageResult = itemService.page(mpPage, null);
        return new PageDTO<>(pageResult.getTotal(), pageResult.getRecords());
    }

    //根据id查询商品
    @GetMapping("/{id}")
    public Item selectById(@PathVariable("id") Long id) {
        log.info("id:{}", id);
        return itemService.getById(id);
    }

    //添加商品
    @PostMapping
    public Boolean save(@RequestBody Item item) {
//        log.info("");
        boolean save = itemService.save(item);
        return save;
    }
    //修改状态
    @PutMapping("/status/{id}/{status}")
    public void seitchStatus(@PathVariable Long id,@PathVariable Integer status){

//            Item item = new Item();
//            item.setId(id);
//            item.setStatus(status);
//            boolean result = itemService.updateById(item);
//            return result;
        UpdateWrapper<Item> wrapper = new UpdateWrapper<>();
        wrapper.set("status",status);   //set starts =#{status}
        wrapper.eq("id",id);    //where id =# {id}
        wrapper.ne("status",3); //starts !=3
        itemService.update(wrapper);
        if (1 == status.intValue()){
            //上架
            rabbitTemplate.convertAndSend(MqConstant.ITEM_EXCHANGE,MqConstant.ITEM_INSERT_KEY,id.toString());
        }else if (2 == status.intValue()){
            //下架
            rabbitTemplate.convertAndSend(MqConstant.ITEM_EXCHANGE,MqConstant.ITEM_DELETE_KEY,id.toString());
        }



    }

    //修改
    @PutMapping
    public Boolean update(@RequestBody Item item){
        log.info("item:{}",item);
        boolean result = itemService.updateById(item);
        return result;
    }



    //根据id删除
    @DeleteMapping("/{id}")
    public Boolean DeleteById(@PathVariable Long id){
    log.info("id:{}",id);
      return   itemService.removeById(id);
    }



    @PutMapping("/stock/{itemId}/{num}")
    public void stock(@PathVariable("itemId") long itemId ,@PathVariable("num") Integer num){

        Item item = itemService.getById(itemId);

        Integer status = item.getStatus();

        itemService.updateStock(itemId, num);


    }



}
